package com.example.a321.rxnetdemo;

import android.os.Bundle;
import android.widget.TextView;

import com.facebook.stetho.Stetho;
import com.orhanobut.logger.LogLevel;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import io.realm.Realm;
import io.realm.RealmConfiguration;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MainActivity extends BaseActivity {
    TextView tv;
    public static MainActivity instance;
    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Stetho.initializeWithDefaults(this);
        instance = this;
        // The RealmConfiguration is created using the builder pattern.
        // The Realm file will be located in Context.getFilesDir() with name "myrealm.realm"
        RealmConfiguration config = new RealmConfiguration.Builder(this)
                .name("user.realm")
                .schemaVersion(42)
                .build();
        Realm.setDefaultConfiguration(config);
        // Use the config
        setContentView(R.layout.activity_main);
        Logger.init(this.getClass().getSimpleName())                 // default PRETTYLOGGER or use just init()
                .methodCount(3)                 // default 2
                .hideThreadInfo()               // default shown
                .logLevel(LogLevel.FULL)        // default LogLevel.FULL
                .methodOffset(2);             // default 0
        tv = (TextView) findViewById(R.id.tv);
        findViewById(R.id.button).setOnClickListener(view -> {
            type = "today";
            getData();
        });
        findViewById(R.id.button2).setOnClickListener(view -> {
            type = "tomorrow";
            getData();
        });
    }

    @Override
    protected void getData() {
        super.getData();
        DemoApi demoService = DemoService.createDemoService();
//        demoService.getDemoResponse(Constant.appKey, "天蝎座", type)
//                .delay(1500, TimeUnit.MILLISECONDS)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(this::handlerSuccess, this::handlerFailure);
        PostEntity postEntity = new PostEntity();
        postEntity.setDescription("描述车安徽省的卡上");
        postEntity.setPolicy_name("测试策略123213");
        postEntity.setTemplate_name("测试模板");
        PostEntity.ContentsBean contentsBean = new PostEntity.ContentsBean();
        PostEntity.ContentsBean.ConditionBean conditionBean = new PostEntity.ContentsBean.ConditionBean();
        conditionBean.setDate_gte(1470180389000l);
        conditionBean.setDate_lte(1473180389000l);
        List<String> strings = new ArrayList<>();
        strings.add("learning:plan:tag:23");
        strings.add("learning:exercise:id:128,31,11,1,1111,1231");
        strings.add("learning:anwser:id:*");
        strings.add("video:live:id:3");
        strings.add("video:live:tag:free");
        strings.add("video:video:id:*");
        strings.add("download:docs:tag:free,reading");
        strings.add("mall:order:id:3,2,21,344");
        contentsBean.setResource(strings);
        contentsBean.setAction("*");
        contentsBean.setEffect("allow");
        contentsBean.setCondition(conditionBean);
        List<PostEntity.ContentsBean> contentsBeenList = new ArrayList<>();
        contentsBeenList.add(contentsBean);
        postEntity.setContents(contentsBeenList);
        demoService.postData(postEntity).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this::handlerSuccess, this::handlerFailure);
    }

    @Override
    protected void handlerSuccess(BaseEntity<? extends BaseEntity> entity) {
        super.handlerSuccess(entity);
        tv.setText(entity.toString() + "---"+entity.getResultcode());

    }

    @Override
    protected void handlerFailure(Throwable throwable) {
        super.handlerFailure(throwable);
        tv.setText(throwable.getMessage());
    }
}
